package com.java.comprehensive.thread.threa_1;

import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadPoolExecutor;

public  class  ScmcommissionLineServiceImpl{




    //导入数据
    public  void importData(List  list) throws InterruptedException {
        //500分一组
        int count = 500;
        int listSize = list.size();
        //线程数
        int RunSize = (listSize / count)+1;

        ThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(RunSize);
        CountDownLatch countDownLatch = new CountDownLatch(RunSize);

        for (int i = 0; i < RunSize; i++) {
            List  newList = null ;
            if((i+1)==RunSize){
                int startIndex = (i*count);;
                int endIndex = list.size();
                newList =list.subList(startIndex,endIndex);
            }else{
                int startIndex = i*count;;
                int endIndex = (i+1)*count-1;
                newList =list.subList(startIndex,endIndex);
            }

            ImportTask task= new ImportTask (newList,countDownLatch);
            executor.execute(task);
        }

        countDownLatch.await();  //主线程等待所有线程完成任务
        //所有线程完成任务后的一些业务
        System.out.println("插入数据完成!");
//                   .....
        //关闭线程池
        executor.shutdown();

    }



}


class ImportTask implements Runnable{
    private List  list;
    private CountDownLatch countDownLatch;
    public ImportTask (List list,CountDownLatch countDownLatch){
        this.list  = list ;
        this.countDownLatch=countDownLatch;
    }

    @Override
    public void run() {
        if(null!=list){
            //业务逻辑

        }
        countDownLatch.countDown();//发出线程任务完成的信号

    }
}